<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    x: {{ x }}
    y: {{ y }}
    <button @click="handleAdd">add</button>
    <span>now count is {{ count }}</span>
  </div>
  <script type="module">
    import { createApp, reactive, onMounted, onUnmounted, toRefs, ref } from './node_modules/vue/dist/vue.esm-browser.js'

    const useMouseMove = () => {
      // 第一个参数 props
      // 第二个参数 context：attrs、emit、slots
      const position = reactive({
        x: 0,
        y: 0,
      })

      const update = e => {
        position.x = e.pageX
        position.y = e.pageY
      }

      onMounted(() => {
        window.addEventListener('mousemove', update)
      })

      onUnmounted(() => {
        window.removeEventListener('mousemove', update)
      })

      return toRefs(position)
    }

    const useCount = () => {
      const count = ref(0)
      return {
        count,
        handleAdd: () => {
          count.value++
        },
      }
    }

    const app = createApp({
      setup () {
        const { x, y } = useMouseMove()
        return {
          x,
          y,
          ...useCount(),
        }
      }
    })

    app.mount('#app')
  </script>
</body>
</html>